<?php namespace App\Filters;

use App\Models\AttributeProperty;
use App\Models\Region;
use EloquentFilter\ModelFilter;

class PropertyFilter extends ModelFilter
{
    /**
     * @var bool
     * Doing this would allow you to have a company() filter method as well as a companyId()
     */
    protected $drop_id = false;
    /**
     * @var bool
     * Doing this would allow you to have a mobile_phone() filter method instead of mobilePhone()
     */
    protected $camel_cased_methods = false;
    /**
    * Related Models that have ModelFilters as well as the method on the ModelFilter
    * As [relationMethod => [input_key1, input_key2]].
    *
    * @var array
    */
    public $relations = [];

    public function town_id($town_id)
    {
        if (count($town_id)){
            return $this->whereIn('town_id', $town_id);
        }
    }
    public function area($area)
    {
        if ($area > 0){
            $ids = Region::getIds($area);
            return $this->whereIn('area',$ids);
        }
    }
    public function metro_station_id($metro_station_id)
    {
        if(count($metro_station_id)){
            return $this->whereIn('metro_station_id',$metro_station_id);
        }
    }

    public function metro_id($metro_id)
    {
        if ($metro_id > 0){
            return $this->where('metro_id',$metro_id);
        }
    }

    public function title($keyword)
    {
        return $this->whereLike('title', $keyword);
    }

    public function wytype($wytype)
    {
        if(count($wytype)){
            return $this->whereIn('wytype', $wytype);
        }
    }

    public function renovation_id($renovation_id)
    {
        if(count($renovation_id)) {
            return $this->whereIn('renovation_id', $renovation_id);
        }
    }

    public function average($average)
    {
        $price = explode('_',$average);
        if(min($price)>1){
            return $this->whereBetween('average',[min($price),max($price)]);
        }
    }

    public function apartment($apartment)
    {
        if (count($apartment)){
            $ids = AttributeProperty::getPropertyByAttrId($apartment);
            return $this->whereIn('id',$ids);
        }
    }

    public function feature($feature)
    {
        if (count($feature)){
            $ids = AttributeProperty::getPropertyByAttrId($feature);
            return $this->whereIn('id',$ids);
        }
    }
}
